#include <catch2/catch.hpp>

#include <array>

#include "solution.hpp"

using namespace solution_002;

/**
 * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
 * Output: 7 -> 0 -> 8
 * Explanation: 342 + 465 = 807.
 */

TEST_CASE("2. Add Two Numbers", "[002]") {

SECTION("example 1") {
    std::array l1{ ListNode(2), ListNode(4), ListNode(3), };
    std::array l2{ ListNode(5), ListNode(6), ListNode(4), };
    for (size_t i = 0; i + 1 < l1.size(); ++i)
        l1[i].next = &l1[i + 1];
    for (size_t i = 0; i + 1 < l2.size(); ++i)
        l2[i].next = &l2[i + 1];

    auto l3 = Solution().addTwoNumbers(&l1[0], &l2[0]);
    for (auto v : {7,0,8}) {
        CHECK(l3->val == v);
        auto next = l3->next;
        delete l3;
        l3 = next;
    }
}

SECTION("example 2") {
    std::array l1{ ListNode(0) };
    std::array l2{ ListNode(0) };
    auto l3 = Solution().addTwoNumbers(&l1[0], &l2[0]);
    for (auto v : {0}) {
        CHECK(l3->val == v);
        auto next = l3->next;
        delete l3;
        l3 = next;
    }
}

SECTION("example 3") {
    std::array<ListNode, 7> l1;
    std::array<ListNode, 4> l2;
    for (size_t i = 0; i + 1 < l1.size(); ++i) {
        l1[i].next = &l1[i + 1];
        l1[i].val = l1[i + 1].val = 9;
    }
    for (size_t i = 0; i + 1 < l2.size(); ++i) {
        l2[i].next = &l2[i + 1];
        l2[i].val = l2[i + 1].val = 9;
    }

    auto l3 = Solution().addTwoNumbers(&l1[0], &l2[0]);
    for (auto v : {8,9,9,9,0,0,0,1}) {
        CHECK(l3->val == v);
        auto next = l3->next;
        delete l3;
        l3 = next;
    }
}

}
